package com.wzq.animal.fore.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.animal.common.Result;
import com.wzq.animal.fore.pojo.ScenicSpot;
import com.wzq.animal.fore.pojo.ScenicSpot;
import com.wzq.animal.fore.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/scenic")
public class ScenicSpotController {
    @Autowired
    private ScenicSpotService scenicSpotService;

    /**
     * 获取景区信息
     * @return
     */
    @GetMapping("/getScenic")
    public Result getScenic(){
        List<ScenicSpot> scenicSpots = scenicSpotService.list();
        return Result.success(scenicSpots);
    }

    @GetMapping("/getPages/{currentPage}/{pageSize}")
    public Result getPages(@PathVariable("currentPage") int currentPage,
                           @PathVariable("pageSize") int pageSize,
                           @RequestParam("nameKeywords") String nameKeywords){
        Page<ScenicSpot> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<ScenicSpot> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(nameKeywords)) {
            queryWrapper.like(ScenicSpot::getName, nameKeywords);
        }
        scenicSpotService.page(page, queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/addOrUpdateScenicSpot")
    public Result addOrUpdateScenicSpot(@RequestBody ScenicSpot spot) {
        scenicSpotService.saveOrUpdate(spot);
        Integer id = spot.getId();
        if(StringUtils.isNotBlank(id.toString())){
            ScenicSpot updatedScenicSpot = scenicSpotService.getById(id);
            return Result.success(updatedScenicSpot);
        }
        return Result.success();
    }

    @DeleteMapping("/delScenicSpot/{id}")
    public Result delScenicSpot(@PathVariable("id") int id) {
        scenicSpotService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/delScenicSpotBatch")
    public Result delBatch(@RequestBody List<Integer> ids) {
        scenicSpotService.removeByIds(ids);
        return Result.success();
    }
}
